package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.service.AppVerSionService;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppVerSion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 版本升级
 */
@Controller
@RequestMapping("/appversion")
public class AppVersionController extends ClientBaseController {


    @Autowired
    private AppVerSionService versionsService;

    @RequestMapping("/banben")
    @ResponseBody
    public Result banben(Integer qudaoid, Integer verint) {
        try {

//            if (qudaoid == null) {
//                return this.errorJSON("渠道ID不能为空");
//            }
//
//            if (qudaoid != 1 && qudaoid != 2 && qudaoid != 3) {
//                return this.errorJSON("渠道ID非法参数!");
//            }
//
//            if (verint == null) {
//                return this.errorJSON("版本号不能为空");
//            }

            Map paraMap = new HashMap(16);
            paraMap.put("state", Constants.SHENGXIAO_STATE_1);
            paraMap.put("qudaoid", qudaoid);
            List<AppVerSion> appVerSionList = versionsService.find(paraMap, null);
            if (appVerSionList != null && appVerSionList.size() > 0) {
                AppVerSion ver = appVerSionList.get(0);
                if (ver.getVerint() > verint) {
                    return this.sucessJSON(ver, "获取成功！");
                }
            }
            return this.sucessJSON(new AppVerSion(), "获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙，请您稍后重试！");
        }
    }

    /**
     * @Description 获取最新的app下载链接
     * @Param: [qudaoid,//渠道 1、安卓  2、ios]
     * @return: com.vf.admin.common.entity.Result
     * @Author: xzx
     * @Date: 2021/07/23
     */
    @RequestMapping("/theLatestAppDownloadLink")
    @ResponseBody
    public Result theLatestAppDownloadLink(Integer type) {
        try {
            if (type == null) {
                return this.errorJSON("类型不能为空");
            }
            if (type != 1 && type != 2) {
                return this.errorJSON("类型错误");
            }
            Map paraMap = new HashMap(16);
            paraMap.put("state", Constants.SHENGXIAO_STATE_1);
            paraMap.put("qudaoid", type);
            List<AppVerSion> appVerSionList = versionsService.find(paraMap, null);
            if (appVerSionList != null && appVerSionList.size() > 0) {
                AppVerSion ver = appVerSionList.get(0);
                return this.sucessJSON(ver, "成功");
            }
            return this.sucessJSON(new AppVerSion(), "成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }

}
